package com.css.dzj.dzjap.modules.lawenforcesuper.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.enterprise.entity.ApEnterpriseCreditEntity;
import com.css.dzj.dzjap.modules.info.entity.ApExpertEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.ApSupersiveExpertInfoEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.service.AllSuperviseService;
import com.css.dzj.dzjap.modules.lawenforcesuper.service.SupersiveExpertService;
import com.css.dzj.dzjap.modules.publick.entity.ApPublicEntity;
import com.css.dzj.dzjap.modules.record.approject.entity.ApRecordEntity;
import com.css.dzj.dzjap.modules.record.approject.service.ApRecordService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 全覆盖抽取信息controller
 * @author limanman
 */
@RestController
@RequestMapping("allSupervise")
public class AllSuperviseController {
    @Autowired
    private AllSuperviseService allSuperviseService;
    @Autowired
    private ApRecordService apRecordService;
    @Autowired
    private SupersiveExpertService supersiveExpertService;
    @Autowired
    BaseService baseService;
    /**
     * 保存抽取专家的两张表
     * @param list
     * @return
     */
    @PostMapping("/saveAllSupersive")
    public R saveAllSupersive(@RequestBody List<ApSupersiveExpertInfoEntity> list,HttpServletRequest request) {
        try {
            R r = allSuperviseService.saveAllSupersive(list,request);
            if (r.getCode()=="500"){
                baseService.logSuccess(3,"保存成功","2");
                return R.error(r.getMsg());
            }else {
                baseService.logFailed(3,"保存失败","2");
                return R.ok();
            }
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3,"保存失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 查询抽取的专家
     * @param recordId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/queryAllSupersiveOnly")
    public R queryAllSupersiveOnly(@RequestParam(name = "recordId",required = false) String recordId,
                                      @RequestParam(name = "pageNo",required = false) Integer pageNo,
                                      @RequestParam(name = "pageSize",required = false) Integer pageSize){
        try {
            List<ApSupersiveExpertInfoEntity> apSupersiveExpertInfoEntities = allSuperviseService.queryAllSupersiveOnly(recordId);
            PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
            QueryResults<ApExpertEntity> apExpertEntities = allSuperviseService.queryExpertByIdAll(pageRequest,recordId);
            baseService.logSuccess(3,"查询成功","2");
            return R.ok().put("data",apExpertEntities).put("apSupersiveExpertInfoEntities",apSupersiveExpertInfoEntities);
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3,"查询失败","2");
            return R.error();
        }
    }

    @PostMapping("/queryById")
    public R queryById(@RequestParam(name = "recordId",required = false) String recordId){
        try {
            List<ApSupersiveExpertInfoEntity> apSupersiveExpertInfoEntities = allSuperviseService.queryAllSupersiveOnly(recordId);
            baseService.logSuccess(3,"查询成功","2");
            return R.ok().put("data",apSupersiveExpertInfoEntities);
        }catch (Exception e){
            baseService.logFailed(3,"查询失败","2");
            e.printStackTrace();
            return R.error();
        }
    }
    /**
     * 添加进公告
     * @param apPublicEntity
     * @return
     */
    @PostMapping("/saveSupersiveNotice")
    public R saveSupersiveNotice(@RequestBody ApPublicEntity apPublicEntity,HttpServletRequest request){
        try {
            String flag = allSuperviseService.saveSupersiveNotice(apPublicEntity,request);
            if (!"0".equals(flag)) {
                R r = new R();
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);
                baseService.logSuccess(3,"保存成功","2");
                return r;
            } else {
                baseService.logFailed(3,"保存失败","2");
                return R.error("添加失败");
            }
        }catch (Exception e){
            baseService.logFailed(3,"保存失败","2");
            e.printStackTrace();
            return  R.error(e.getMessage());
        }
    }
}
